﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Log4NetLogger.cs" company="MockingBird Tools">
//   Santosh Benjamin
// </copyright>
// <summary>
//   This is a simple log4net based logger.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Diagnostics;
using System.IO;

namespace MockingBird.Common.Infrastructure.Logging
{
    using System;

    // logging library
    using log4net;

    using MockingBird.Common.Infrastructure.Configuration;

    /// <summary>
    /// This is a simple log4net based logger.
    /// </summary>
    public class Log4NetLogger : LoggerBase
    {
        protected static readonly ILog log = LogManager.GetLogger(typeof(Log4NetLogger));
        private const string fileName = "log4net.config";
        private static object configureLockObject = new object();

        #region "constructors"
        public Log4NetLogger()
        {
            this.SetDefaults();
            this.SetLogFileName();
            this.ConfigureLogger();
        }

        public Log4NetLogger(ILoggingConfigurationProvider targetLoggingConfigProvider)
        {
            this.loggingConfigurationProvider = targetLoggingConfigProvider;
            this.loggerConfigFileFolder = targetLoggingConfigProvider.LoggingConfigurationFolder;
            this.SetLogFileName();
            this.ConfigureLogger();
            Trace.WriteLine("Configure Logging");
        }
        #endregion


        #region ILogger Members

        public override void LogRequestReceived(string endpointUrl)
        {
            log.Debug("REQUEST RECEIVED FOR ENDPOINT: " + endpointUrl);
            Trace.WriteLine("REQUEST RECEIVED FOR ENDPOINT: " + endpointUrl);
        }

        public override void Log(string message)
        {

            log.Debug(message);
            Trace.WriteLine(message);
        }

        public override void LogInfo(string message)
        {
            log.Info(message);
            Trace.WriteLine(message);
        }

        public override void LogWarning(string message)
        {
            log.Warn(message);
            Trace.WriteLine(message);
        }

        public override void LogError(string message, Exception ex)
        {
            log.Error(message + Environment.NewLine + ex.ToString(), ex);
            Trace.WriteLine(message + Environment.NewLine + ex.ToString());
        }

        public override void LogFatal(string message, Exception ex)
        {
            log.Fatal(message, ex);
            Trace.WriteLine(message + Environment.NewLine + ex.ToString());

        }

        #endregion

        #region "private methods"

        /// <summary>
        /// Any defaults that are not overriden through specialized constructors etc
        /// </summary>
        private void SetDefaults()
        {
            this.loggerConfigFileFolder = ConfigurationUtils.GetSiblingConfigFolder();
        }
        private void SetLogFileName()
        {
            this.loggingConfigFile = Path.Combine(this.loggerConfigFileFolder, fileName);
        }

        private void ConfigureLogger()
        {
            if (!File.Exists(this.loggingConfigFile))
            {
                throw new InvalidOperationException(string.Format("Target logging configuration file does not exist {0}", this.loggingConfigFile));
            }

            FileInfo loggingConfigInfo = new FileInfo(this.loggingConfigFile);
            lock (configureLockObject)
            {
                log4net.Config.XmlConfigurator.Configure(loggingConfigInfo);
            }


        }

        #endregion
    }
}
